<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of OgHtmlclass
 *
 * @author martin
 */
class OgHtmlDocument {

  protected $layout;
  private   $header;
  private   $body;

  public function  __construct() {
    $this->initComponents();
  }


  /**
   * Gets instance of Html header
   *
   * @return OgElement
   */
  public function getHeader() {
    if(!is_object($this->header)) {
      $this->header = new OgElement('head');
      $this->getLayout()->addComponent($this->header);
    }
    return $this->header;
  }

  /**
   * Gets instance of Html body
   *
   * @return OgElement
   */
  public function getBody() {
    if(!is_object($this->body)) {
      $this->body = new OgElement('body');
      $this->getLayout()->addComponent($this->body);
    }
    return $this->body;
  }

  /**
   *
   * @param <type> $name
   * @return OgElement
   */
  public function getComponent($name) {
    return OgPointers::singleton()->getPointer($name);
  }


  /**
   *
   * @return OgElement
   */
  private function getLayout() {
    if(!is_object($this->layout)) {
      $this->layout = new OgElement('html');
    }
    return $this->layout;
  }


  public function draw() {
    $this->layout->toString();
  }

  /**
   * Print recursive structure of this template instance
   */
  public function prdLayout() {
    OgDevHelp::prd($this->layout);
  }


  private function initComponents() {
    $this->layout = new OgElement('html');
    $this->getHeader();
    $this->getBody();
  }

  public function toString($element = null, $tab_level = 0) {
    if(!$element) $element = $this->layout;
    $tabs = OgHtml::tabs($tab_level);

    print $tabs.OgHtml::startTag($element->getTagName(), $element->mergeAttributes());
    if(is_array($element->getInnerHtml())) {
      $tab_level++;
      foreach ($element->getInnerHtml() as $html) {
        if(is_object($html)) {
          print "\n";
          $this->toString($html, $tab_level);
          print $tabs;
        }
        else {
          print $html;
        }
      }
    }
    print OgHtml::endTag($element->getTagName());
    if(is_string($html)) print "\n";
  }

  public function preRender() {
    $this->maked_string = "";
    $this->toBString();
    return $this->maked_string;
  }


  public function toBString($element = null, $tab_level = 0) {
    if(!$element) $element = $this->layout;
    //if(!$this->maked_string)  $this->maked_string = '';
    $tabs = OgHtml::tabs($tab_level);

    $this->maked_string .= $tabs.OgHtml::startTag($element->getTagName(), $element->mergeAttributes());
    if(is_array($element->getInnerHtml())) {
      $tab_level++;
      foreach ($element->getInnerHtml() as $html) {
        if(is_object($html)) {
          $this->maked_string .= "\n";
          $this->toBString($html, $tab_level);
          $this->maked_string .= $tabs;
        }
        else {
          $this->maked_string .= $html;
        }
      }
    }
    $this->maked_string .= OgHtml::endTag($element->getTagName());
    if(is_string($html)) $this->maked_string .= "\n";
  }


}
?>
